package com.hm.PushCaseService.service.ydcg.caseinfo.replenish;

import com.alibaba.fastjson2.JSON;
import com.hm.PushCaseService.constant.ReqUrl;
import com.hm.PushCaseService.pojo.CaseBean;
import com.hm.PushCaseService.pojo.ydcg.ReplenishBean;
import okhttp3.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.StringJoiner;

@Service
public class ReplenishCaseService implements IReplenishCase {
    private static final Logger logger = LoggerFactory.getLogger(ReplenishCaseService.class);

    @Qualifier("OkHttpClient")
    public OkHttpClient okHttpClient;

    public ReplenishCaseService(OkHttpClient okHttpClient) {
        this.okHttpClient = okHttpClient;
    }

    @Override
    public void replenish(CaseBean caseBean) {
        String eventCode = caseBean.eventcode;
        String reqUrl = ReqUrl.ReqSupplementUrl + "?taskNum=" + eventCode;
        Request request = new Request.Builder().url(reqUrl).build();
        try (Response response = okHttpClient.newCall(request).execute()) {
            ResponseBody body = response.body();
            if (body == null) {
                //请求失败
                logger.error("任务号：" + eventCode + "body为空");
                return;
            }
            String jsonString = body.string();
            ReplenishBean replenishBean = JSON.parseObject(jsonString, ReplenishBean.class);
            List<ReplenishBean.Data.RecInfo.Medias> mediaList = replenishBean.data.recInfo.medias;
            String disposePerson = replenishBean.data.recInfo.disposePerson;//当前处理部门
            String actDefName = replenishBean.data.recInfo.actDefName;//当前处理人
            StringJoiner stringJoiner = new StringJoiner(",");//图片信息
            for (ReplenishBean.Data.RecInfo.Medias medias : mediaList) {
                String mediaURL = medias.mediaURL.replaceAll("null", ReqUrl.PicHost);
                stringJoiner.add(mediaURL);
            }
            //开始对CaseBean补充复制
            caseBean.fileUrls = stringJoiner.toString();//图片
            caseBean.currentDeptName = disposePerson;//当前处理部门
            caseBean.currentDealPeopleName = actDefName;//当前处理人
        } catch (Exception e) {
            logger.error(eventCode + "获取补充案件失败：Exception:" + e.getMessage());
        }

    }
}
